package application.dispatcher;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;

import org.dsrg.soenea.domain.command.CommandException;
import org.dsrg.soenea.domain.mapper.DomainObjectNotFoundException;

import domain.command.ViewCurrentGroupCommand;
import domain.model.user.User;
import domain.model.user.mapper.UserInputMapper;


public class ViewCurrentGroupDispatcher extends Dispatcher{

	@Override
	public void execute() throws ServletException, IOException {
		
		
		try 
		{
			//Shouldnt have domain logic in here but can't think of any other way to check if the user is in a group without it...
			
			User user = UserInputMapper.find((Long)(myHelper.getSessionAttribute("userid")));
			
			if(user.getGroup().getId() != 0) // if the user is in a group
			{
				
			new ViewCurrentGroupCommand(myHelper).execute();
			myHelper.setRequestAttribute("viewFile", "ViewCurrentGroupTV");
			forward("/WEB-INF/JSP/template/template.jsp");
			}
			else
			{
				relativeRedirect("browsegroups");
			}
		
		} 
		 catch (DomainObjectNotFoundException e) {

			 forward("JSP/Errors.jsp");
		} catch (SQLException e) {

			forward("JSP/Errors.jsp");
		}
		catch (CommandException e) 
		{
			forward("JSP/Errors.jsp");
			//e.printStackTrace();
		}
		
		
		
	}

	
	
	
}
